﻿Public Class frmLSnippet
    Public bAllowClose As Boolean = False
    Private myWU As New clsWU
    Private myHistoryForm As frmHistory
#Region "Set form state"
    Public Delegate Sub dWindowState(fState As FormWindowState)
    Public Sub SetWindowState(State As FormWindowState)
        If Me.InvokeRequired Then
            Dim nI As New dWindowState(AddressOf SetWindowState)
            nI.Invoke([State])
        Else
            Me.WindowState = State
        End If
    End Sub
    Public Delegate Sub dSetEnabled(Enabled As Boolean)
    Public Sub SetEnabled(Enabled As Boolean)
        If Me.InvokeRequired Then
            Dim nI As New dSetEnabled(AddressOf SetEnabled)
            nI.Invoke([Enabled])
        Else
            Me.Enabled = Enabled
        End If
    End Sub
#End Region
    Public Sub CloseForm()
        Try
            bAllowClose = True
            Me.Close()
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Public Sub ShowSnippet(WU As clsWU, Optional HistoryForm As frmHistory = Nothing)
        Try
            If Not IsNothing(HistoryForm) Then
                myHistoryForm = HistoryForm
            End If
            myWU = WU
            Me.Text = myWU.PRCG & vbTab & " topmost=" & Me.TopMost.ToString
            rtLog.Clear()
            rtLog.SuspendLayout()
            rtLog.Text = WU.Report & Environment.NewLine & Environment.NewLine
            rtLog.AppendText(WU.CoreSnippet & Environment.NewLine)
            If WU.bHasRestarted Then
                rtLog.AppendText(Environment.NewLine)
                For Each rInfo In WU.restartInfo.RestartInfo
                    rtLog.AppendText("Work unit has been restarted at percentage " & rInfo.LastFrame.strPercentage & " at " & rInfo.LastFrame.FrameDT.ToString & Environment.NewLine)
                    rtLog.AppendText("Corestatus = " & rInfo.CoreStatus & Environment.NewLine)
                Next
            End If
            rtLog.SelectionStart = 0
            rtLog.ScrollToCaret()
            rtLog.ResumeLayout()
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub frmLSnippet_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not bAllowClose Then
                e.Cancel = True
                Me.Hide()
            End If
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub rtLog_MouseDoubleClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtLog.MouseDoubleClick
        Try
            Me.TopMost = Not Me.TopMost
            Me.Text = myWU.PRCG & vbTab & " topmost=" & Me.TopMost.ToString
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub frmLSnippet_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Try
            'Check!
            If Not IsNothing(myHistoryForm) Then
                'Allow going through the wu list with left and right
                If e.KeyData = Keys.Right Or e.KeyData = Keys.Left Then
                    If e.KeyData = Keys.Left And myHistoryForm.lvWU.SelectedIndices(0) > 1 Then
                        myHistoryForm.lvWU.Items(myHistoryForm.lvWU.SelectedIndices(0) - 1).Selected = True
                    ElseIf e.KeyData = Keys.Right And myHistoryForm.lvWU.SelectedIndices(0) < myHistoryForm.lvWU.Items.Count - 1 Then
                        myHistoryForm.lvWU.Items(myHistoryForm.lvWU.SelectedIndices(0) + 1).Selected = True
                    End If
                    e.SuppressKeyPress = True
                    'Return focus to prevent beep
                    Me.Focus()
                End If
            End If
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub TopMostClicked()

    End Sub

    Private Sub frmLSnippet_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Try
            'Check!
            If Not IsNothing(myHistoryForm) Then
                'Allow going through the wu list with left and right
                If e.KeyData = Keys.Right Or e.KeyData = Keys.Left Then
                    If e.KeyData = Keys.Left And myHistoryForm.lvWU.SelectedIndices(0) > 1 Then
                        myHistoryForm.lvWU.Items(myHistoryForm.lvWU.SelectedIndices(0) - 1).Selected = True
                    ElseIf e.KeyData = Keys.Right And myHistoryForm.lvWU.SelectedIndices(0) < myHistoryForm.lvWU.Items.Count - 1 Then
                        myHistoryForm.lvWU.Items(myHistoryForm.lvWU.SelectedIndices(0) + 1).Selected = True
                    End If
                    e.SuppressKeyPress = True
                    'Return focus to prevent beep
                    Me.Focus()
                End If
            End If
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub frmLSnippet_PreviewKeyDown(sender As Object, e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles Me.PreviewKeyDown
        Try
            If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then e.IsInputKey = True
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub CopyToClipboardToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CopyToClipboardToolStripMenuItem.Click
        Try
            Clipboard.SetText(rtLog.Text)
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub

    Private Sub CloseFormToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CloseFormToolStripMenuItem.Click
        Try
            Me.Hide()
        Catch ex As Exception
            WriteError(ex.Message, Err)
        End Try
    End Sub
    Private Sub TopMostToolStripMenuItem_CheckedChanged(sender As Object, e As System.EventArgs) Handles TopMostToolStripMenuItem.CheckedChanged
        Try
            Me.TopMost = TopMostToolStripMenuItem.Checked
        Catch ex As Exception

        End Try
    End Sub

End Class